#include "formabout.h"
#include <QFile>
#include <QTextStream>
#include <QMessageBox>

#include "rssfactory.h"
#include "appfactory.h"


FormAbout::FormAbout(const QString &license, const QString &authors, const QString &changelog, QWidget *parent) : QDialog(parent), ui(new Ui::FormAbout) {
    ui->setupUi(this);

	ui->lblVersion->setText(QString("<a href=\"%1\">Check for latest version</a>").arg(APP_URL));
	connect(ui->lblVersion, SIGNAL(linkActivated(QString)), this, SLOT(checkVersion(QString)));

    QTextStream str;
    QFile file;
    str.setDevice(&file);

    file.setFileName(license);
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    ui->txtLicense->setText(str.readAll());
    file.close();

    file.setFileName(authors);
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    ui->txtThanks->setText(str.readAll());
    file.close();

    file.setFileName(changelog);
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    ui->txtChangelog->setText(str.readAll());
    file.close();

    ui->lblDesc->setText(tr("<b>RSS Guard</b><br>"
                            "<b>Version:</b> %1 (build on %2 with CMake %3)<br>"
                            "<b>Revision:</b> %4<br>"
                            "<b>Build date:</b> %5 (%6)<br>"
                            "<b>Target framework:</b> Qt %7").arg(qApp->applicationVersion(),
                                                                  CMAKE_SYSTEM,
                                                                  CMAKE_VERSION,
                                                                  QString(APP_REVISION).toUpper(),
                                                                  __DATE__,
                                                                  __TIME__,
                                                                  QT_VERSION_STR));

    ui->txtInfo->setText(tr("RSS Guard is a tool for reading online RSS 0.92/1.0/2.0 & ATOM 1.0 feeds. It can be minimized to notification area and it can automatically look for new messages and notice you if there are any."
                            "\n\nThis software is distributed under GNU GPLv3 license."
                            "\n\nRSS Guard is made by Martin Rotter."
							"\n\nMy contacts are:\n  - rotter.martinos@gmail  ~email\n  - code.google.com/p/rss-guard ~ rss guard's website\n  - www.martin-rotter.8u.cz  ~author's website"
                            "\n\nYou can obtain source code for RSS Guard from its website."
                            "\n\n\nCopyright ©  2011 Martin Rotter"));
}

FormAbout::~FormAbout() {
    delete ui;
}

void FormAbout::checkVersion(const QString &url) {
	// Obtain latest stable version number.
	QString version = AppFactory::latestVersion(url);

    qWarning() << version;

	// Display everything in messagebox.
	QMessageBox::information(this,
							 tr("New version check"),
							 tr("<b>%1</b><br><br>"
								"Installed version: %2<br>"
								"Latest stable version: %3").arg(APP_VERSION == version ?
																	 tr("No new version available.") :
																	 tr("You are not using latest stable version."),
																 APP_VERSION,
																 version));
}
